﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using LifeSoftLibary;
using System.Data.SqlClient;
using LifeSoft.GUI;
using LifeSoft.Utils;
using LifeSoft.GUI.usrControl;
using LifeSoft.BusinessObject;
using Microsoft.Win32;
using LifeSoft.DataLayer;
using Microsoft.SqlServer.Management.Smo;
using Microsoft.SqlServer.Management.Common;
using crmquanlynhansu.GUI.usrControl;
using crmquanlynhansu.GUI;
using crmquanlynhansu;
using System.Drawing;
using System.Collections.Generic;
using System.ComponentModel;

namespace LifeSoft
{
    public partial class frmMain : Form
    {
        MetrolBillCommands _Commands = null;
        private long sysUserId = 0L;
        private MenuStrip menuStrip;
        private ToolStripMenuItem menuItem;
        private ToolStripPanel panel;
        private ToolStrip toolStrip;

        public frmMain()
        {
            InitializeComponent();

            menuStrip = new MenuStrip();
            menuItem = new ToolStripMenuItem();
            panel = new ToolStripPanel();
            panel = new ToolStripPanel();
            toolStrip = new ToolStrip();
            this.menuStrip.SuspendLayout();
            this.panel.SuspendLayout();
            this.toolStrip.SuspendLayout();

            RegistryKey hKey = Registry.LocalMachine;
            RegistryKey hKSoft = hKey.OpenSubKey("Software");
            RegistryKey hKMine = hKSoft.OpenSubKey("LifeSoft", true);
            if (hKMine != null)
            {
                string strUserId = hKMine.GetValue("SysUserId").ToString();
                if (strUserId != null && strUserId != "")
                {
                    sysUserId = long.Parse(strUserId);
                }
                hKMine.Close();
            }
            hKSoft.Close();
        }

        private void exit_Click(object sender, EventArgs e)
        {
            DialogResult results;
            results = MessageBox.Show("Bạn có muốn sao lưu lại dữ liệu trước khi thoát không?", 
                "Thoát ứng dụng!", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
            if (results == DialogResult.Yes)
            {
                String strName = new CommonUtils().AutoStrName();
                saveFileDialogBackup.FileName = "Backup" + strName + ".bak";
                saveFileDialogBackup.Filter = "Backup File (*.bak)|*.bak";
                saveFileDialogBackup.Title = "File location";
                saveFileDialogBackup.InitialDirectory = AppDomain.CurrentDomain.BaseDirectory;
                if (saveFileDialogBackup.ShowDialog() == DialogResult.OK)
                {
                    new DBUtils().BackupBAK("QUANLYKHO", saveFileDialogBackup.FileName);
                    MessageBox.Show("Sao lưu dữ liệu thành công!", "Sao lưu cơ sở dữ liệu", MessageBoxButtons.OK);
                    this.Close();
                }
            }
            else if (results == DialogResult.No)
            {
                this.Close();
                RegistryKey hKey = Registry.LocalMachine;
                RegistryKey hKSoft = hKey.OpenSubKey("Software");
                RegistryKey hMine = hKSoft.OpenSubKey("LifeSoft", true);
                hMine.SetValue("SysUserId", 0);
                hMine.Close();
            }
        }

        private void RestoreDB_Click(object sender, EventArgs e)
        {

        }

        private void helpContent_Click(object sender, EventArgs e)
        {
            frmHelp help = new frmHelp();
            help.ShowDialog();
        }

        private void websiteNSP_Click(object sender, EventArgs e)
        {

        }

        private void teamView_Click(object sender, EventArgs e)
        {

        }

        private void about_Click(object sender, EventArgs e)
        {
            frmAbout about = new frmAbout();
            about.ShowDialog();
        }

        public void frmMain_Load(object sender, EventArgs e)
        {
            if (sysUserId != null && sysUserId != 0)
            {
                usrMain main = new usrMain();
                this.paneShowMain.Controls.Add(main);
                main.Dock = DockStyle.Fill;
                login.Visible = false;
                SysLoginInfo info = new SysLoginInfo();
                info = new LoginData().GLoginInfo(sysUserId);
                if (info != null)
                {
                    toolStrUserInfo.Text = info.lastName + " " + info.firstName;
                    serverName.Text = "localhost";
                }
            }
            else
            {

                frmLogin login = new frmLogin();
                menuList.Visible = false;
                menuService.Visible = false;
                menuSystem.Visible = false;
                menuUtilities.Visible = false;
                toolBar.Visible = false;
                paneMenu.Visible = false;

                login.ShowDialog();

                //kiem tra xem 
                Boolean checkNullBase = new DBUtils().getBaseUnit();
                if (checkNullBase == true)
                {
                    ctrlSetupFirstBase ctr = new ctrlSetupFirstBase();
                    this.Controls.Add(ctr);
                }
            }
        }

        protected override void OnFormClosing(FormClosingEventArgs e)
        {
            if (sysUserId != null && sysUserId != 0)
            {
                base.OnFormClosing(e);
                if (e.CloseReason == CloseReason.WindowsShutDown) return;
                DialogResult results;
                results = MessageBox.Show("Bạn có muốn sao lưu lại dữ liệu trước khi thoát không?",
                                          "Thoát ứng dụng!", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
                if (results == DialogResult.Yes)
                {

                    String strName = new CommonUtils().AutoStrName();
                    saveFileDialogBackup.FileName = "Backup" + strName + ".bak";
                    saveFileDialogBackup.Filter = "Backup File (*.bak)|*.bak";
                    saveFileDialogBackup.Title = "File location";
                    saveFileDialogBackup.InitialDirectory = AppDomain.CurrentDomain.BaseDirectory;
                    if (saveFileDialogBackup.ShowDialog() == DialogResult.OK)
                    {
                        new DBUtils().BackupBAK("QUANLYKHO", saveFileDialogBackup.FileName);
                        MessageBox.Show("Sao lưu dữ liệu thành công!", "Sao lưu cơ sở dữ liệu", MessageBoxButtons.OK);
                        e.Cancel = false;
                        RegistryKey hKey = Registry.LocalMachine;
                        RegistryKey hKSoft = hKey.OpenSubKey("Software");
                        RegistryKey hMine = hKSoft.OpenSubKey("LifeSoft", true);
                        hMine.SetValue("SysUserId", 0);
                        hMine.Close();
                    }
                }
                else if (results == DialogResult.Cancel)
                {
                    e.Cancel = true;
                }
            }
        }

        public void login_Click(object sender, EventArgs e)
        {
            frmLogin frm = new frmLogin();
            frm.ShowDialog();
            ctrlSetupFirstBase ctrl = new ctrlSetupFirstBase();
            this.paneShowMain.Controls.Remove(ctrl);
        }

        public void login_success()
        {
            Application.Restart();
            menuList.Visible = true;
            menuService.Visible = true;
            menuSystem.Visible = true;
            menuUtilities.Visible = true;
            toolBar.Visible = true;
            paneMenu.Visible = true;
        }

        private void website_Click(object sender, EventArgs e)
        {

        }

        private void paneMain_Click(object sender, EventArgs e)
        {
            paneShowMain.Controls.Clear();
            usrMain main = new usrMain();
            paneShowMain.Controls.Add(main);
            main.Dock = DockStyle.Fill;
            this.toolStripMenu.Visible = false;
        }

        private void paneSellGood_Click(object sender, EventArgs e)
        {
            paneShowMain.Controls.Clear();
            usrSellGood sellGood = new usrSellGood();
            paneShowMain.Controls.Add(sellGood);
            sellGood.Dock = DockStyle.Fill;
            this.toolStripMenu.Visible = false;
        }

        private void panePurchase_Click(object sender, EventArgs e)
        {
            paneShowMain.Controls.Clear();
            usrPurchase purchase = new usrPurchase();
            paneShowMain.Controls.Add(purchase);
            purchase.Dock = DockStyle.Fill;
            this.toolStripMenu.Visible = false;
        }

        private void paneContract_Click(object sender, EventArgs e)
        {
            paneShowMain.Controls.Clear();
            usrContract contract = new usrContract();
            paneShowMain.Controls.Add(contract);
            contract.Dock = DockStyle.Fill;
            this.toolStripMenu.Visible = false;
        }

        private void paneWareHouse_Click(object sender, EventArgs e)
        {
            paneShowMain.Controls.Clear();
            usrWareHouse wareHouse = new usrWareHouse();
            paneShowMain.Controls.Add(wareHouse);
            wareHouse.Dock = DockStyle.Fill;
            this.toolStripMenu.Visible = false;
        }

        private void paneStaff_Click(object sender, EventArgs e)
        {
            this.toolStripMenu.Visible = false;
            paneShowMain.Controls.Clear();
            usrStaff staff = new usrStaff();
            paneShowMain.Controls.Add(staff);
            staff.Dock = DockStyle.Fill;
            staff.ShowListDepartment = new usrStaff.showListDepartment(department_Click);
        }

        public void department_Click(listDepartment department)
        {
            department = new listDepartment();
            paneShowMain.Controls.Clear();
            paneShowMain.Controls.Add(department);
            department.Dock = DockStyle.Fill;
            this.toolStripMenu.Visible = true;
            AddNew.Click += new EventHandler(UpdateDepartment);
        }

        private void UpdateDepartment(object sender, EventArgs e)
        {
            frmDepartment department = new frmDepartment();
            department.ShowDialog();
        }

        private void Search_Click(object sender, EventArgs e)
        {

        }

    }

}
